home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / mpeg_play-2.1 / gdith-old.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-09  |  15.9 KB  |  776 lines

  1. /*
  2.  * Copyright (c) 1995 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #include <math.h>
  22. #include "video.h"
  23. #include "proto.h"
  24. #include "dither.h"
  25.  
  26. /* Range values for lum, cr, cb. */
  27. int LUM_RANGE;
  28. int CR_RANGE;
  29. int CB_RANGE;
  30.  
  31. /* Array that remaps color numbers to actual pixel values used by X server. */
  32.  
  33. unsigned char pixel[256];
  34.  
  35. /* Arrays holding quantized value ranged for lum, cr, and cb. */
  36.  
  37. int *lum_values;
  38. int *cr_values;
  39. int *cb_values;
  40.  
  41. /* Declaration of global variable containing dither type. */
  42.  
  43. extern int ditherType;
  44.  
  45. /* Structures used by the X server. */
  46.  
  47. Display *display;
  48.  
  49. static XImage *ximage = NULL;
  50. static Colormap cmap;
  51. static Window window;
  52. static GC gc;
  53.  
  54.  
  55.  
  56. /*
  57.  *--------------------------------------------------------------
  58.  *
  59.  * InitColor --
  60.  *
  61.  *    Initialized lum, cr, and cb quantized range value arrays.
  62.  *
  63.  * Results: 
  64.  *      None.
  65.  *
  66.  * Side effects:
  67.  *      None.
  68.  *
  69.  *--------------------------------------------------------------
  70.  */
  71.  
  72. void
  73. InitColor()
  74. {
  75.   int i;
  76.  
  77.   for (i=0; i<LUM_RANGE; i++) {
  78.     lum_values[i] = ((i * 256) / (LUM_RANGE)) + (256/(LUM_RANGE*2));
  79.   }
  80.  
  81.   for (i=0; i<CR_RANGE; i++) {
  82.     cr_values[i] = ((i * 256) / (CR_RANGE)) + (256/(CR_RANGE*2));
  83.   }
  84.  
  85.   for (i=0; i<CB_RANGE; i++) {
  86.     cb_values[i] = ((i * 256) / (CB_RANGE)) + (256/(CB_RANGE*2));
  87.   }
  88.  
  89. }
  90.  
  91.  
  92. /*
  93.  *--------------------------------------------------------------
  94.  *
  95.  * ConvertColor --
  96.  *
  97.  *    Given a l, cr, cb tuple, converts it to r,g,b.
  98.  *
  99.  * Results:
  100.  *    r,g,b values returned in pointers passed as parameters.
  101.  *
  102.  * Side effects:
  103.  *      None.
  104.  *
  105.  *--------------------------------------------------------------
  106.  */
  107.  
  108. static void
  109. ConvertColor(l, cr, cb, r, g, b)
  110.      unsigned char l, cr, cb;
  111.      unsigned char *r, *g, *b;
  112. {
  113.   double fl, fcr, fcb, fr, fg, fb;
  114.  
  115.   fl = 1.164*(((double) l)-16.0);
  116.   fcr =  ((double) cr) - 128.0;
  117.   fcb =  ((double) cb) - 128.0;
  118.  
  119.  
  120.   fr = fl + (1.596 * fcb);
  121.   fg = fl - (0.813 * fcb) - (0.391 * fcr);
  122.   fb = fl + (2.018 * fcr);
  123.  
  124.   if (fr < 0.0) fr = 0.0;
  125.   else if (fr > 255.0) fr = 255.0;
  126.  
  127.   if (fg < 0.0) fg = 0.0;
  128.   else if (fg > 255.0) fg = 255.0;
  129.  
  130.   if (fb < 0.0) fb = 0.0;
  131.   else if (fb > 255.0) fb = 255.0;
  132.  
  133.   *r = (unsigned char) fr;
  134.   *g = (unsigned char) fg;
  135.   *b = (unsigned char) fb;
  136.  
  137. }
  138.  
  139. #ifdef SH_MEM
  140.  
  141. int gXErrorFlag = 0;
  142.  
  143. int HandleXError(dpy, event)
  144.      Display *dpy;
  145.      XErrorEvent *event;
  146. {
  147.   gXErrorFlag = 1;
  148.  
  149.   return 0;
  150. }
  151.  
  152. void InstallXErrorHandler()
  153. {
  154.   int HandleXError();
  155.  
  156.   XSetErrorHandler(HandleXError);
  157.   XFlush(display);
  158. }
  159.  
  160. void DeInstallXErrorHandler()
  161. {
  162.   XSetErrorHandler(NULL);
  163.   XFlush(display);
  164. }
  165. #endif
  166.  
  167.  
  168. /*
  169.  *--------------------------------------------------------------
  170.  *
  171.  * ResizeDisplay --
  172.  *
  173.  *    Resizes display window.
  174.  *
  175.  * Results:
  176.  *    None.
  177.  *
  178.  * Side effects:
  179.  *      None.
  180.  *
  181.  *--------------------------------------------------------------
  182.  */
  183.  
  184. void ResizeDisplay(w, h)
  185.      int w, h;
  186. {
  187.  
  188.   if (ditherType == NO_DITHER || ditherType == PPM_DITHER) return;
  189.  
  190.   XResizeWindow(display, window, w, h);
  191.   XFlush(display);
  192. }
  193.  
  194.  
  195. /*
  196.  *--------------------------------------------------------------
  197.  *
  198.  * MakeWindow --
  199.  *
  200.  *    Create X Window
  201.  *
  202.  * Results:
  203.  *    Read the code.
  204.  *
  205.  * Side effects:
  206.  *      None.
  207.  *
  208.  *--------------------------------------------------------------
  209.  */
  210.  
  211. #ifdef SH_MEM
  212. int CompletionType = -1;
  213. #endif
  214.  
  215. static void 
  216. MakeWindow(name) 
  217. char *name;
  218. {
  219.   
  220.   XSizeHints hint;
  221.   unsigned int fg, bg;
  222.   char *hello = "MPEG Play";
  223.   int screen;
  224.   Window CreateFullColorWindow();
  225.  
  226.   if (ditherType == NO_DITHER || ditherType == PPM_DITHER) return;
  227.  
  228.   display = XOpenDisplay(name);
  229.   if (display == NULL) {
  230.     fprintf(stderr, "Can not open display\n");
  231.     exit(-2);
  232.   }
  233.  
  234. #ifdef SH_MEM
  235.   if(shmemFlag)
  236.     CompletionType = XShmGetEventBase(display) + ShmCompletion;
  237. #endif
  238.  
  239.   screen = DefaultScreen (display);
  240.   
  241.   /* Fill in hint structure */
  242.  
  243.   hint.x = 200;
  244.   hint.y = 300;
  245.   hint.width = 150;
  246.   hint.height = 150;
  247.   hint.flags = PPosition | PSize;
  248.   
  249.   /* Get some colors */
  250.   
  251.   bg = WhitePixel (display, screen);
  252.   fg = BlackPixel (display, screen);
  253.   
  254.   /* Make the window */
  255.   
  256.   if (ditherType == FULL_COLOR_DITHER || ditherType==FULL_COLOR2_DITHER) {
  257.     window = CreateFullColorWindow (display, hint.x, hint.y, hint.width, hint.height);
  258.     if (window == 0) {
  259.       fprintf (stderr, "-color option only valid on full color display\n");
  260.       exit (-1);
  261.     }
  262.   } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  263.     window = XCreateSimpleWindow (display,
  264.                   DefaultRootWindow (display),
  265.                   hint.x, hint.y,
  266.                   hint.width, hint.height,
  267.                   4, fg, bg);
  268.   } else {
  269.     XVisualInfo vinfo;
  270.     
  271.     if (!XMatchVisualInfo (display, screen, 8, PseudoColor, 
  272.                &vinfo)) {
  273.  
  274.       if (!XMatchVisualInfo(display, screen, 8, GrayScale, 
  275.                 &vinfo)) {
  276.  
  277.     fprintf(stderr, "-requires 8 bit display\n");
  278.     exit(-1);
  279.       }
  280.     }
  281.  
  282.     window = XCreateSimpleWindow (display,
  283.                  DefaultRootWindow (display),
  284.                  hint.x, hint.y,
  285.                  hint.width, hint.height,
  286.                  4, fg, bg);
  287.   }
  288.   
  289.   XSelectInput(display, window, StructureNotifyMask);
  290.  
  291.   /* Tell other applications about this window */
  292.   
  293.   XSetStandardProperties (display, window, hello, hello, None, NULL, 0, &hint);
  294.   
  295.   /* Map window. */
  296.  
  297.   XMapWindow(display, window);
  298.  
  299.   /* Wait for map. */
  300.   while(1) {
  301.     XEvent    xev;
  302.  
  303.     XNextEvent(display, &xev);
  304.     if(xev.type == MapNotify && xev.xmap.event == window)
  305.       break;
  306.   }
  307.   XSelectInput(display, window, NoEventMask);
  308. }
  309.   
  310.  
  311. /*
  312.  *--------------------------------------------------------------
  313.  *
  314.  * InitDisplay --
  315.  *
  316.  *    Initialized display, sets up colormap, etc.
  317.  *
  318.  * Results:
  319.  *      None.
  320.  *
  321.  * Side effects:
  322.  *      None.
  323.  *
  324.  *--------------------------------------------------------------
  325.  */
  326.  
  327. void InitDisplay(name)
  328. char *name;
  329. {
  330.  
  331.   int ncolors = LUM_RANGE*CB_RANGE*CR_RANGE;
  332.   XColor xcolor;
  333.   int i, lum_num, cr_num, cb_num;
  334.   unsigned char r, g, b;
  335.   Colormap dcmap;
  336.  
  337.   if (ditherType == NO_DITHER) return;
  338.   if (noDisplayFlag) return;
  339.  
  340.   MakeWindow(name);
  341.  
  342.   gc = XCreateGC(display, window, 0, 0);
  343.  
  344.   dcmap = cmap = XDefaultColormap(display, DefaultScreen(display));
  345.  
  346.   xcolor.flags = DoRed | DoGreen | DoBlue;
  347.  
  348.   if (owncmFlag) goto create_map;
  349.   retry_alloc_colors:
  350.   for (i=0; i<ncolors; i++) {
  351.  
  352.     lum_num = (i / (CR_RANGE*CB_RANGE))%LUM_RANGE;
  353.     cr_num = (i / CB_RANGE)%CR_RANGE;
  354.     cb_num = i % CB_RANGE;
  355.  
  356.     ConvertColor(lum_values[lum_num], cr_values[cr_num], cb_values[cb_num], &r, &g, &b);
  357.  
  358.     xcolor.red = r * 256;
  359.     xcolor.green = g * 256;
  360.     xcolor.blue = b * 256;
  361.  
  362.     if(XAllocColor(display, cmap, &xcolor) == 0 && cmap == dcmap) {
  363.       int j;
  364.       long tmp_pixel;
  365.       XWindowAttributes xwa;
  366.  
  367.       if (!quietFlag) {
  368.     fprintf(stderr, "Using private colormap.\n");
  369.       }
  370.  
  371.       /* Free colors. */
  372.       for(j = 0; j < i; j ++) {
  373.     tmp_pixel = pixel[j];
  374.         XFreeColors(display, cmap, &tmp_pixel, 1, 0);
  375.       }
  376.  
  377.       create_map:
  378.       XGetWindowAttributes(display, window, &xwa);
  379.       cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  380.       XSetWindowColormap(display, window, cmap);
  381.  
  382.       goto retry_alloc_colors;
  383.     }
  384.     pixel[i] = xcolor.pixel;
  385.   }
  386.  
  387.   ximage = NULL;
  388. }
  389.  
  390.  
  391. /*
  392.  *--------------------------------------------------------------
  393.  *
  394.  * InitGrayDisplay --
  395.  *
  396.  *    Initialized display for gray scale dither.
  397.  *
  398.  * Results:
  399.  *      None.
  400.  *
  401.  * Side effects:
  402.  *      None.
  403.  *
  404.  *--------------------------------------------------------------
  405.  */
  406.  
  407. void InitGrayDisplay(name)
  408. char *name;
  409. {
  410.   int ncolors = 128;
  411.   XColor xcolor;
  412.   int i;
  413.   Colormap dcmap;
  414.  
  415.   MakeWindow(name);
  416.  
  417.   gc = XCreateGC(display, window, 0, 0);
  418.  
  419.   dcmap = cmap = XDefaultColormap(display, DefaultScreen(display));
  420.  
  421.   xcolor.flags = DoRed | DoGreen | DoBlue;
  422.  
  423.   if (owncmFlag) goto create_map;
  424.   retry_alloc_grays:
  425.   for (i=0; i<ncolors; i++) {
  426.  
  427.     xcolor.red = (i*2) * 256;
  428.     xcolor.green = (i*2) * 256;
  429.     xcolor.blue = (i*2) * 256;
  430.  
  431.     if(XAllocColor(display, cmap, &xcolor) == 0 && cmap == dcmap) {
  432.       int j;
  433.       long tmp_pixel;
  434.       XWindowAttributes xwa;
  435.  
  436.       if (!quietFlag) {
  437.     fprintf(stderr, "Using private colormap.\n");
  438.       }
  439.  
  440.       /* Free colors. */
  441.       for(j = 0; j < i; j ++) {
  442.     tmp_pixel = pixel[j*2];
  443.         XFreeColors(display, cmap, &tmp_pixel, 1, 0);
  444.       }
  445.  
  446.       create_map:
  447.       XGetWindowAttributes(display, window, &xwa);
  448.       cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  449.       XSetWindowColormap(display, window, cmap);
  450.  
  451.       goto retry_alloc_grays;
  452.     }
  453.     pixel[(i*2)] = xcolor.pixel;
  454.     pixel[(i*2)+1] = xcolor.pixel;
  455.   }
  456.  
  457.   ximage = NULL;
  458. }
  459.  
  460. /*
  461.  *--------------------------------------------------------------
  462.  *
  463.  * InitGray256Display --
  464.  *
  465.  *    Initialized display for gray scale dither with 256 levels
  466.  *
  467.  * Results:
  468.  *      None.
  469.  *
  470.  * Side effects:
  471.  *      None.
  472.  *
  473.  *--------------------------------------------------------------
  474.  */
  475.  
  476.  
  477. void InitGray256Display(name)
  478. char *name;
  479. {
  480.   int ncolors = 256;
  481.   XColor xcolor;
  482.   int i;
  483.   Colormap dcmap;
  484.   int result;
  485.   XWindowAttributes xwa;
  486.  
  487.   MakeWindow(name);
  488.  
  489.   gc = XCreateGC(display, window, 0, 0);
  490.  
  491.   dcmap = cmap = XDefaultColormap(display, DefaultScreen(display));
  492.  
  493.   xcolor.flags = DoRed | DoGreen | DoBlue;
  494.  
  495.   if (owncmFlag) {
  496.     XGetWindowAttributes(display, window, &xwa);
  497.     cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  498.     XSetWindowColormap(display, window, cmap);
  499.   }
  500.   retry_alloc_grays:
  501.   for (i=0; i<ncolors; i++) {
  502.     xcolor.red = i * 256;
  503.     xcolor.green = i * 256;
  504.     xcolor.blue = i * 256;
  505.     if((result=XAllocColor(display, cmap, &xcolor)) == 0 && cmap == dcmap) {
  506.       int j;
  507.       long tmp_pixel;
  508.  
  509.       if (!quietFlag) {
  510.     fprintf(stderr, "Using private colormap.\n");
  511.       }
  512.  
  513.       /* Free colors. */
  514.       for(j = 0; j < i; j ++) {
  515.     tmp_pixel = pixel[j];
  516.         XFreeColors(display, cmap, &tmp_pixel, 1, 0);
  517.       }
  518.  
  519.       XGetWindowAttributes(display, window, &xwa);
  520.       cmap = XCreateColormap(display, window, xwa.visual, AllocNone);
  521.       XSetWindowColormap(display, window, cmap);
  522.  
  523.       goto retry_alloc_grays;
  524.     }
  525.     pixel[i] = xcolor.pixel;
  526.   }
  527.  
  528.   ximage = NULL;
  529. }
  530.  
  531.  
  532. /*
  533.  *--------------------------------------------------------------
  534.  *
  535.  * InitMonoDisplay --
  536.  *
  537.  *    Initialized display for monochrome dither.
  538.  *
  539.  * Results:
  540.  *      None.
  541.  *
  542.  * Side effects:
  543.  *      None.
  544.  *
  545.  *--------------------------------------------------------------
  546.  */
  547.  
  548. void InitMonoDisplay(name)
  549. char *name;
  550. {
  551.   XGCValues xgcv;
  552.  
  553.   MakeWindow(name);
  554.  
  555.   xgcv.background = BlackPixel(display, DefaultScreen(display));
  556.   xgcv.foreground = WhitePixel(display, DefaultScreen(display));
  557.  
  558.   gc = XCreateGC(display, window, GCForeground | GCBackground, &xgcv);
  559.  
  560.   ximage = NULL;
  561. }
  562.  
  563.  
  564. /*
  565.  *--------------------------------------------------------------
  566.  *
  567.  * InitColorDisplay --
  568.  *
  569.  *    Initialized display for full color output.
  570.  *
  571.  * Results:
  572.  *      None.
  573.  *
  574.  * Side effects:
  575.  *      None.
  576.  *
  577.  *--------------------------------------------------------------
  578.  */
  579.  
  580. void InitColorDisplay(name)
  581. char *name;
  582. {
  583.  
  584.   MakeWindow(name);
  585.  
  586.   gc = XCreateGC(display, window, 0, 0);
  587.   ximage = NULL;
  588. }
  589.  
  590.  
  591. /*
  592.  *--------------------------------------------------------------
  593.  *
  594.  * ExecuteDisplay --
  595.  *
  596.  *    Actually displays display plane in previously created window.
  597.  *
  598.  * Results:
  599.  *    None.
  600.  *
  601.  * Side effects:
  602.  *    None.
  603.  *
  604.  *--------------------------------------------------------------
  605.  */
  606.  
  607. void
  608. ExecuteDisplay(vid_stream)
  609.      VidStream *vid_stream;
  610. {
  611.   char dummy;
  612.   Visual *FindFullColorVisual();
  613.   Visual *fc_visual;
  614.   int depth;
  615.  
  616.   totNumFrames++;
  617.   if (!quietFlag) {
  618.     fprintf (stderr, "%d\r", totNumFrames);
  619.   }
  620.  
  621.   if (partialFlag)
  622.     if (!((totNumFrames>=startFrame) && 
  623.       ((endFrame==-1) || (totNumFrames<=endFrame))))
  624.       return;
  625.   if (requireKeypressFlag) {
  626.     char foo;
  627.     printf("Press return (%d) ",totNumFrames);    
  628.     while ((foo=getchar())!='\n');
  629.   }
  630.  
  631.   if (ditherType == NO_DITHER) return;
  632.   if (ditherType == PPM_DITHER) {
  633.     ExecutePPM(vid_stream);
  634.     return;
  635.   }
  636.  
  637.   if (ximage == NULL) {
  638.     
  639.     if (ditherType == Twox2_DITHER) {
  640.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  641.                 vid_stream->mb_width * 32,
  642.                 vid_stream->mb_height * 32, 8, 0);
  643.     } else if (ditherType == FULL_COLOR_DITHER) {
  644.       fc_visual = FindFullColorVisual(display, &depth);
  645.       ximage = XCreateImage (display, fc_visual, depth, ZPixmap,
  646.                  0, &dummy, vid_stream->mb_width * 16,
  647.                  vid_stream->mb_height * 16, 32, 0);
  648.     } else if (ditherType == FULL_COLOR2_DITHER) {
  649.       fc_visual = FindFullColorVisual(display, &depth);
  650.       ximage = XCreateImage (display, fc_visual, depth, ZPixmap,
  651.                  0, &dummy, vid_stream->mb_width * 32,
  652.                  vid_stream->mb_height * 32, 32, 0);
  653.     } else if (ditherType == MONO_DITHER || ditherType == MONO_THRESHOLD) {
  654.       ximage = XCreateImage (display, None, 1, XYBitmap, 0, &dummy,
  655.                  vid_stream->mb_width * 16,
  656.                  vid_stream->mb_height * 16, 8, 0);
  657.       ximage->byte_order = MSBFirst;
  658.       ximage->bitmap_bit_order = MSBFirst;
  659.     } else {
  660.       ximage = XCreateImage(display, None, 8, ZPixmap, 0, &dummy,
  661.                 vid_stream->mb_width * 16,
  662.                 vid_stream->mb_height * 16, 8, 0);
  663.     }
  664.   }
  665.   
  666.   if (!noDisplayFlag) {
  667. #ifdef SH_MEM
  668.     if (shmemFlag) {
  669.       XShmPutImage(display, window, gc, vid_stream->current->ximage, 
  670.            0, 0, 0, 0,
  671.            vid_stream->current->ximage->width, 
  672.            vid_stream->current->ximage->height, True);
  673.       XFlush(display);
  674.       
  675.       while(1) {
  676.     XEvent xev;
  677.     
  678.     XNextEvent(display, &xev);
  679.     if(xev.type == CompletionType)
  680.       break;
  681.       }
  682.     }
  683.     else 
  684. #endif
  685.       
  686.       {
  687.     ximage->data = (char *) vid_stream->current->display; 
  688.     
  689.     XPutImage(display, window, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height);
  690.       }
  691.   }
  692. }
  693.  
  694.  
  695. extern char *inputName;
  696. extern char *strrchr();
  697. #define BITS 8
  698.  
  699. /*
  700.  *--------------------------------------------------------------
  701.  *
  702.  * ExecutePPM --
  703.  *
  704.  *    Write out a display plane as a PPM file.
  705.  *
  706.  * Results:
  707.  *    None.
  708.  *
  709.  * Side effects:
  710.  *    None.
  711.  *
  712.  *--------------------------------------------------------------
  713.  */
  714.  
  715. void
  716. ExecutePPM(vid_stream)
  717.      VidStream *vid_stream;
  718. {
  719.   static int munged = 0;
  720.   static char mungedInputName[300];
  721.   char fileName[300];
  722.   FILE *file;
  723.   int n;
  724.   unsigned int *p;
  725.   unsigned int r, g, b;
  726.  
  727.   if (!munged) {
  728.     char *cp;
  729.  
  730.     cp = strrchr(inputName, '/');
  731.     if (cp != NULL)
  732.       ++cp;
  733.     else
  734.       cp = inputName;
  735.     strcpy(mungedInputName, cp);
  736.     cp = strrchr(mungedInputName, '.');
  737.     if (cp != NULL)
  738.     *cp = '\0';
  739.     munged = 1;
  740.   }
  741.  
  742.   sprintf(fileName, "%s_%05d.ppm", mungedInputName, totNumFrames );
  743.   file = fopen(fileName, "w");
  744.   if (file == NULL) {
  745.     perror(fileName);
  746.     exit(1);
  747.   }
  748.  
  749.   fprintf(file, "P6\n");
  750.   fprintf(file, "%d %d\n", vid_stream->h_size, vid_stream->v_size);
  751.   fprintf(file, "255\n");
  752.  
  753.   p = (unsigned int *) vid_stream->current->display;
  754.   n = vid_stream->h_size * vid_stream->v_size;
  755.   while (n > 0) {
  756. #ifndef BGR
  757.     r = *p & 0xff;
  758.     g = (*p >> BITS) & 0xff;
  759.     b = (*p >> (2*BITS)) & 0xff;
  760. #else
  761.     b = *p & 0xff;
  762.     g = (*p >> BITS) & 0xff;
  763.     r = (*p >> (2*BITS)) & 0xff;
  764. #endif
  765.     putc(r, file);
  766.     putc(g, file);
  767.     putc(b, file);
  768.     ++p;
  769.     --n;
  770.   }
  771.  
  772.   fclose(file);
  773. }
  774.  
  775.  
  776.